package com.samsung.diarymap;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.samsung.list.ListItemViewActivity;

public class ItemOverLayDraw extends ItemizedOverlay {



	private ArrayList<MyOverlayItem> mOverlays = new ArrayList<MyOverlayItem>();
	private Paint linePaint;
	private Point pre;
	private GeoPoint prePoint;
	private Point endPoint;
	private GeoPoint currentPoint;
	private MapView mapView;
	private Context mContext;
	private Boolean paintLine;
	private Drawable drawable;

	public ItemOverLayDraw(Drawable defaultMarker,Context context) {

		super(boundCenterBottom(defaultMarker));
		mContext = context;
		drawable = defaultMarker;
		Rect rect = defaultMarker.copyBounds();
		// TODO Auto-generated constructor stub
	}
	
	public void addOverLay(MyOverlayItem item)
	{
		mOverlays.add(item);
	}
	
	public void setIsPaintLine(Boolean paintLine)
	{
		
		this.paintLine = paintLine;
		
	}
	
	@Override
	protected OverlayItem createItem(int i) {
		// TODO Auto-generated method stub
		if(!mOverlays.isEmpty())
			return mOverlays.get(i);
		else
			return null;
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		// TODO Auto-generated method stub
		super.draw(canvas, mapView, shadow);
		
		int size = mOverlays.size();
		if(size==0) return;
		
		if(paintLine)
		{
			setUpPaintLine();
			
			endPoint = new Point();
			pre = new Point();
			
			mapView.getProjection().toPixels(mOverlays.get(0).getPoint(), pre);
			
			for(int i=1; i<size; i++){
				endPoint = new Point();
				mapView.getProjection().toPixels(mOverlays.get(i).getPoint(), endPoint);
				canvas.drawLine(pre.x, pre.y, endPoint.x, endPoint.y, linePaint);
				Paint circlePaint = new Paint();
				Typeface mType;
				mType = Typeface.create(Typeface.DEFAULT_BOLD, Typeface.NORMAL);
				
				Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
				mPaint.setTextSize(16);
				mPaint.setTypeface(mType);
	//			canvas.drawText("Here is my Home!", pre.x+10, pre.y, mPaint);
				circlePaint.setColor(Color.RED);
				canvas.drawCircle(pre.x, pre.y, 5, circlePaint);
				pre = endPoint;
			}
		}else{
			setUpOverlayItem(mapView);
		}

//		drawAt(canvas, drawable, 100, 100, false);
		
//		super.draw(canvas, mapView, shadow);
		
//		Point screenCoords = new Point();
//		Point screenCoords1 = new Point();
//		
//		mapView.getProjection().toPixels(prePoint, screenCoords);
//		int x1=screenCoords.x;
//		int y1=screenCoords.y;
//		
//		mapView.getProjection().toPixels(currentPoint, screenCoords1);
//		int x2=screenCoords1.x;
//		int y2=screenCoords1.y;
//		setUpPaintLine();
//		canvas.drawLine(x1, y1, x2, y2, linePaint);
	}
	
	private void setUpOverlayItem(MapView mapView) {
		ArrayList<MyOverlayItem> list = mOverlays;
		for(int i=0; i<mOverlays.size(); i++){
			MyOverlayItem item = list.get(i);
			if(item.getType()==1){
				
				Point pixelPoint = new Point();
				GeoPoint gp = item.getGeopoint();
				GeoPoint originalgp = gp;
				
				long date = item.getSetDate();
				list.remove(item);
				
				pixelPoint = mapView.getProjection().toPixels(gp, pixelPoint);
				pixelPoint.x+=30;
				pixelPoint.y+=5;
				
//				gp = new GeoPoint((int)c.getLatitude(), (int)c.getLongitude()-150);
				gp = mapView.getProjection().fromPixels(pixelPoint.x, pixelPoint.y);
				MyOverlayItem overlayitem = new MyOverlayItem(gp, "call", "from call message");
//				item.setGeopoint(gp);
				
				overlayitem.setSetDate(date);
				overlayitem.setType(1);
				overlayitem.setGeopoint(originalgp);
				addOverLay(overlayitem);
			}
			if(item.getType()==2){
				
				Point pixelPoint = new Point();
				GeoPoint gp = item.getGeopoint();
				GeoPoint originalgp = gp;
				
				long date = item.getSetDate();
				list.remove(item);
				
				pixelPoint = mapView.getProjection().toPixels(gp, pixelPoint);
				pixelPoint.x-=30;
				pixelPoint.y+=5;
				
//				gp = new GeoPoint((int)c.getLatitude(), (int)c.getLongitude()-150);
				gp = mapView.getProjection().fromPixels(pixelPoint.x, pixelPoint.y);
				MyOverlayItem overlayitem = new MyOverlayItem(gp, "call", "from call message");
//				item.setGeopoint(gp);
				
				overlayitem.setSetDate(date);
				overlayitem.setType(2);
				overlayitem.setGeopoint(originalgp);
				addOverLay(overlayitem);
			}
			if(item.getType()==3){
				
				Point pixelPoint = new Point();
				GeoPoint gp = item.getGeopoint();
				GeoPoint originalgp = gp;
				
				long date = item.getSetDate();
				list.remove(item);
				
				pixelPoint = mapView.getProjection().toPixels(gp, pixelPoint);
				pixelPoint.y-=25;
				
//				gp = new GeoPoint((int)c.getLatitude(), (int)c.getLongitude()-150);
				gp = mapView.getProjection().fromPixels(pixelPoint.x, pixelPoint.y);
				MyOverlayItem overlayitem = new MyOverlayItem(gp, "call", "from call message");
//				item.setGeopoint(gp);
				
				overlayitem.setSetDate(date);
				overlayitem.setType(3);
				overlayitem.setGeopoint(originalgp);
				addOverLay(overlayitem);
			}
			
		}
		populate();
		// TODO Auto-generated method stub
		
	}

	private void setUpPaintLine() {
		linePaint = new Paint();
		linePaint.setARGB(255, 255, 0, 0);
		linePaint.setStrokeWidth(3);
//		linePaint.setDither(true);
		linePaint.setStyle(Style.FILL);
		linePaint.setAntiAlias(true);
		linePaint.setStrokeJoin(Paint.Join.ROUND);
		linePaint.setStrokeCap(Paint.Cap.ROUND);
		// TODO Auto-generated method stub
	}
	
	@Override
	public int size() {
		// TODO Auto-generated method stub
		return mOverlays.size();
	}
	
	@Override
	protected boolean onTap(int index) {
		// TODO Auto-generated method stub
		if(mOverlays.isEmpty()) return false;
		
		MyOverlayItem item = mOverlays.get(index);
		
		if(item.getType() != 0)
		{
			Intent intent = new Intent(mContext, ListItemViewActivity.class);
			intent.putExtra("date", item.getSetDate());
			intent.putExtra("type", item.getType());
			mContext.startActivity(intent);
		}
//		Toast.makeText(mContext, item.getSnippet(), Toast.LENGTH_LONG).show();
		return true;
	}
	
	public void callPopulate()
	{
		populate();
	}

}